package com.cq.hd.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.enums.biz.OrderSettleBillTradeStateEnum;
import com.cq.hd.common.enums.biz.OrderSettleBillTradeTypeEnum;
import com.cq.hd.order.api.dto.OrderSettleBillDto;
import com.cq.hd.order.api.vo.OrderSettleBillVo;
import com.cq.hd.order.service.HfTransferRetryService;
import com.cq.hd.order.service.TbOrderSettleBillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author much
 * @since 2024/04/30 23:27
 */
@Slf4j
@Service
public class HfTransferRetryServiceImpl implements HfTransferRetryService {

    @Resource
    private TbOrderSettleBillService orderSettleBillService;

    /**
     * 商户本金失败重试
     */
    @Override
    public void merchantAmtTransferRetry() {
        log.info("HF本金失败重试 start");
        OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
        orderSettleBillDto.setTradeType(OrderSettleBillTradeTypeEnum.MERCHANT_AMT.getValue());
        orderSettleBillDto.setTradeStates(Arrays.asList(OrderSettleBillTradeStateEnum.WAIT.getValue(), OrderSettleBillTradeStateEnum.FAIL.getValue()));
        List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillService.listByDto(orderSettleBillDto);
        if (CollectionUtils.isNotEmpty(orderSettleBillVos)) {
            for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                log.info("HF本金失败重试, item={}", JSON.toJSONString(orderSettleBillVo));
                orderSettleBillService.transactAmt(orderSettleBillVo);
            }
        }
        log.info("HF本金失败重试 end");
    }

}
